home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!sun2!ua302aa
- From: ua302aa@sun2.lrz-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: Command line Arguments
- Date: 4 Feb 1996 21:54:17 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4f39u9$2b6@sparcserver.lrz-muenchen.de>
- References: <4f2qev$9jq@cloner3.netcom.com>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- buxx@ix.netcom.com(Glen 'Steve' Vandiver ) writes:
-
- >Hi! I have been haveing trouble with my commandline arguements. I have
- >it too where i can read the entire argument string after the run. No
- >prob. but lets say i want it to split it up like this. the first word
- >goes into char *user; and the rest goes into char *command;. HOW WOULD
- >I DO THIS? thanx! bye!
-
- You could do something like
- ----------------8<--------------------8<-------------------------
- #include <stdio.h>
- #include <stdlib.h>
-
- int
- main(int argc, char **argv)
- {
- register int i;
- char *user;
- char *cmd = 0;
- size_t len = 0;
-
- if (argc < 3)
- exit(EXIT_FAILURE);
- user = argv[1];
- for (i = 2; i < argc; ++i) {
- len += strlen(argv[i]) + 1;
- cmd = realloc(cmd, len);
- if (cmd == NULL)
- exit(EXIT_FAILURE);
- if (i == 2) {
- strcpy(cmd, argv[i]);
- } else {
- strcat(cmd, " ");
- strcat(cmd, argv[i]);
- }
- }
- printf("call(\"%s\", \"%s\")\n", user, cmd);
- return EXIT_SUCCESS;
- }
- ----------------8<--------------------8<-------------------------
- but most operating environments I know support a method of passing
- more than on "word" as one argument to a program, e.g. something
- like making your user enter
-
- call myprg user "do what ever you want"
-
- might result in myprog being called with two arguments.
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
- | ua302aa@sunmail.lrz-muenchen.de
-